﻿using System;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using LuxuryResort.WebSite.LRMPDataService;

namespace LuxuryResort.WebSite.Members
{
    public partial class ManageReservations : Page
    {
        private readonly RoomServiceClient _svc = new RoomServiceClient();

        private Guid UserID
        {
            get
            {
                if (ViewState["UserID"] == null && HttpContext.Current.User != null)
                {
                    string userName = HttpContext.Current.User.Identity.Name;
                    object userId = Membership.GetUser(userName).ProviderUserKey;
                    ViewState["UserID"] = userId;
                }
                return (Guid) ViewState["UserID"];
            }
        }

        private Guid ResortId
        {
            get { return new Guid(ConfigurationManager.AppSettings["ResortId"]); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void lvReservations_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "EditReservation")
            {
                BindDataToReservation(e.Item);
            }
            if (e.CommandName == "DeleteReservation")
            {
                string hidRSID = ((HiddenField) e.Item.FindControl("hidReservationId")).Value;
                string hidRMID = ((HiddenField) e.Item.FindControl("hidRoomId")).Value;
                _svc.DeleteReservation(new Guid(hidRSID), new Guid(hidRMID), UserID, ResortId);
            }
        }

        protected void DataPagerReservations_PreRender(object sender, EventArgs e)
        {
            try
            {
                lvReservations.DataSource = _svc.GetReservations(UserID, ResortId);
                lvReservations.DataBind();
            }
            catch (Exception ex)
            {
                Response.Redirect("~/AccessDenied.aspx");
            }
        }

        private void BindDataToReservation(ListViewItem item)
        {
            hidRSID.Value = ((HiddenField) item.FindControl("hidReservationId")).Value;
            hidRMID.Value = ((HiddenField) item.FindControl("hidRoomId")).Value;
            hidRoomPrice.Value = ((HiddenField) item.FindControl("hidRoomPrice")).Value;
            //hidStatus.Value = ((HiddenField)item.FindControl("hidStatus")).Value;
            lblRoomType.Text = ((Label) item.FindControl("lblRoomType")).Text;
            lblRoomNumber.Text = ((Label) item.FindControl("lblRoomNo")).Text;
            txtCustomerName.Text = ((Label) item.FindControl("lblCustomerName")).Text;
            txtDateFrom.Text = Convert.ToDateTime(((Label) item.FindControl("lblCheckIn")).Text).ToString("yyyy/MM/dd");
            txtDateTo.Text = Convert.ToDateTime(((Label) item.FindControl("lblCheckOut")).Text).ToString("yyyy/MM/dd");
            cbxPaymentDone.Checked = Convert.ToBoolean(((HiddenField) item.FindControl("hidPaymentDone")).Value);
            ddlStatuses.SelectedValue = ((HiddenField) item.FindControl("hidStatus")).Value;

            divReservationInformation.Visible = true;
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            var reservation = new Reservation
            {
                RSID = new Guid(hidRSID.Value),
                RMID = new Guid(hidRMID.Value),
                CustomerName = txtCustomerName.Text.Trim(),
                PaymentMethod = cboPaymentMethod.SelectedValue,
                Quantity = DateTime.Parse(txtDateTo.Text)
                    .Subtract(DateTime.Parse(txtDateFrom.Text))
                    .Days + 1,
                CheckIn = DateTime.Parse(txtDateFrom.Text),
                CheckOut = DateTime.Parse(txtDateTo.Text),
                PaymentDone = cbxPaymentDone.Checked,
                Status = ddlStatuses.SelectedValue,
                Total = !string.IsNullOrEmpty(hidRoomPrice.Value)
                    ? Convert.ToDecimal(hidRoomPrice.Value)*
                      (DateTime.Parse(txtDateTo.Text).Subtract(DateTime.Parse(txtDateFrom.Text)).Days + 1)
                    : 0
            };


            _svc.UpdateReservation(reservation, UserID, ResortId);

            divReservationInformation.Visible = false;
        }
    }
}